package com.zgis.module.bas.bean;

import com.zgis.module.bas.util.CxWKT;

/**
 * 修改记录:
 * 24-09-29     zhengchaojun             首次创建
 */

/**
 * 类描述：geom图形处理相关
 */
public class CxGeom
{
    private String dbId;

    private int    srid;

    private String wkt;

    /**
     * 构造函数，初始化CxGeom对象。
     *
     * @param dbId    数据库标识符，用于确定数据库类型。
     * @param srid    空间参考系统标识符。
     * @param geoJson GeoJSON格式的字符串。
     * @throws Exception 如果转换过程中发生错误。
     */
    public CxGeom(String dbId, int srid, String geoJson) throws Exception
    {
        this.dbId = dbId;
        this.srid = srid;
        this.wkt = CxWKT.jsonToWkt(geoJson);
    }

    /**
     * 获取WKT格式的字符串。
     *
     * @return WKT格式的字符串。
     */
    public String getWKT()
    {
        return this.wkt;
    }

    /**
     * 获取几何对象的类型。
     *
     * @return 几何对象的类型（POINT, LINE, POLYGON）。
     */
    public String getWKTType()
    {
        String type = "";
        if (this.wkt.indexOf("POINT") == 0)
        {
            type = "POINT";
        }
        else if (this.wkt.indexOf("LINESTRING") == 0)
        {
            type = "LINE";
        }
        else if (this.wkt.indexOf("POLYGON") == 0)
        {
            type = "POLYGON";
        }

        return type;
    }

    /**
     * 获取数据库特定的几何对象字符串。
     *
     * @return 数据库特定的几何对象字符串。
     */
    public String getGeom()
    {
        String geom = null;
        switch (this.dbId)
        {
            case "oracle":
                geom = "MDSYS.SDO_GEOMETRY('" + this.wkt + "'," + this.srid + ")";
                break;
            case "mysql":
            case "postgresql":
            case "dm8":
                geom = "ST_GeomFromText('" + this.wkt + "'," + this.srid + ")";
                break;
            case "sqlserver":
                geom = "geometry::STGeomFromText(geometry::STGeomFromText('" + this.wkt + "'," + this.srid + ")";
        }

        return geom;
    }

    /**
     * 获取数据库特定的缓冲几何对象字符串。
     *
     * @param r 缓冲半径。
     * @return 数据库特定的缓冲几何对象字符串。
     */
    public String getBufferGeom(double r)
    {
        String geom = null;
        switch (this.dbId)
        {
            case "oracle":
                geom = "MDSYS.SDO_GEOMETRY('" + this.wkt + "'," + this.srid + ")";
                break;
            case "mysql":
            case "postgresql":
                String type = this.getWKTType();
                if (type.equals("POINT"))
                {
                    geom = "ST_SetSRID(ST_Buffer(ST_GeomFromText('" + this.wkt + "'," + this.srid + ")," + r + ",'quad_segs=8')," + this.srid + ")";
                }
                else if (type.equals("LINE"))
                {
                    geom = "ST_SetSRID(ST_Buffer(ST_GeomFromText('" + this.wkt + "'," + this.srid + ")," + r + ",'endcap=round join=round')," + this.srid + ")";
                }
                else if (type.equals("POLYGON"))
                {
                    geom = "ST_SetSRID(ST_Buffer(ST_GeomFromText('" + this.wkt + "'," + this.srid + ")," + r + ",'endcap=round join=round')," + this.srid + ")";
                }
                break;
            case "dm8":
                geom = "ST_SetSRID(ST_Buffer(ST_GeomFromText('" + this.wkt + "'," + this.srid + ")," + r + ")," + this.srid + ")";
                break;
            case "sqlserver":
                geom = "geometry::STGeomFromText(geometry::STGeomFromText('" + this.wkt + "'," + this.srid + ")";
        }

        return geom;
    }

    /**
     * 获取范围查询字符串。
     *
     * @param geom 几何对象字符串。
     * @return 范围查询字符串。
     */
    public String getRangeQuery(String geom)
    {
        String query = null;
        switch (this.dbId)
        {
            case "oracle":
                query = "SDO_RELATE(geom," + geom + ",'MASK=ANYINTERACT QUERYTYPE=WINDOW')='TRUE'";
                break;
            case "mysql":
            case "postgresql":
            case "dm8":
                query = "ST_Intersects(geom," + geom + ")";
                break;
            case "sqlserver":
                query = geom + ".STContains(geom) = 1";
        }

        return query;
    }

    /**
     * 获取缓冲查询字符串。
     *
     * @param geom 几何对象字符串。
     * @param r    缓冲半径。
     * @return 缓冲查询字符串。
     */
    public String getBufferQuery(String geom, double r)
    {
        String query = null;
        switch (this.dbId)
        {
            case "oracle":
                query = "SDO_WITHIN_DISTANCE(GEOM," + geom + ", 'DISTANCE=" + r + " UNIT=M') = 'TRUE'";
                break;
            case "mysql":
            case "postgresql":
            case "dm8":
                query = "ST_Intersects(geom," + geom + ")";
                break;
            case "sqlserver":
                query = geom + ".STContains(geom) = 1";
        }

        return query;
    }
}
